package com.layblog.config;

import com.layblog.shiro.AccountRealm;
import com.layblog.shiro.LoginFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.crazycake.shiro.RedisCacheManager;
import org.crazycake.shiro.RedisManager;
import org.crazycake.shiro.RedisSessionDAO;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
@Configuration
public class ShiroConfig {

    /**
     * 注入安全管理器
     */
    //@Bean("securityManager")
    //public SecurityManager securityManager(AccountRealm accountRealm, SessionManager sessionManager, CacheManager cacheManager) {
    //    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    //    securityManager.setSessionManager(sessionManager);
    //    securityManager.setCacheManager(cacheManager);
    //    securityManager.setRealm(accountRealm);
    //    log.info("------------->securityManager注入完成");
    //    return securityManager;
    //}
    @Bean("securityManager")
    public DefaultWebSecurityManager securityManager(AccountRealm accountRealm) {

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(accountRealm);

        log.info("------------------>securityManager注入成功");

        return securityManager;
    }

    /**
     * shiro基础配置
     *
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager securityManager) {

        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
        filterFactoryBean.setSecurityManager(securityManager);
        // 配置登录的url和登录成功的url
        filterFactoryBean.setLoginUrl("/login");
        filterFactoryBean.setSuccessUrl("/user/center");
        // 配置未授权跳转页面
        filterFactoryBean.setUnauthorizedUrl("/error/403");

        Map<String, Filter> filterMap = new HashMap<>();
        filterMap.put("auth", loginFilter());

        filterFactoryBean.setFilters(filterMap);

        Map<String, String> hashMap = new LinkedHashMap<>();
        //静态资源
        hashMap.put("/res/**", "anon");

        hashMap.put("/login", "anon");
        hashMap.put("/post/edit", "auth");      //未登录提醒，这里的额auth其实是过滤器别名，也就是LoginFilter
        hashMap.put("/post/delete", "auth");
        hashMap.put("/post/submit", "auth");
        hashMap.put("/post/reply", "auth");
        hashMap.put("/post/jieda-delete", "auth");
        hashMap.put("/center", "auth");
        hashMap.put("/center/collection", "auth");
        //hashMap.put("/admin/**", "auth, roles[admin]");
        hashMap.put("/admin/initEsData", "anon");
        filterFactoryBean.setFilterChainDefinitionMap(hashMap);

        return filterFactoryBean;
    }

    /**
     * 特别注意，自定义loginFilter要在shiroFilter后面，否则会报错
     *
     * @author: jacklin
     * @since 2022/6/27 0:24
     **/
    @Bean
    public LoginFilter loginFilter() {
        return new LoginFilter();
    }


    ///**
    // * 配置shiro redisManager
    // * 使用的是shiro-redis开源插件
    // *
    // * @return
    // */
    //@Bean
    //public RedisManager redisManager() {
    //    RedisManager redisManager = new RedisManager();
    //    redisManager.setHost(host);
    //    redisManager.setDatabase(database);
    //    return redisManager;
    //}
    //
    //@Bean
    //RedisSessionDAO redisSessionDAO() {
    //    RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
    //    redisSessionDAO.setRedisManager(redisManager());
    //    return redisSessionDAO;
    //}
    //
    //@Bean
    //DefaultWebSessionManager sessionManager(RedisSessionDAO redisSessionDAO) {
    //    DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
    //    sessionManager.setSessionDAO(redisSessionDAO);
    //    return sessionManager;
    //}
    //
    //@Bean
    //RedisCacheManager redisCacheManager() {
    //    RedisCacheManager redisCacheManager = new RedisCacheManager();
    //    redisCacheManager.setRedisManager(redisManager());
    //    //redis中针对不同用户缓存(此处的id需要对应user实体中的id字段,用于唯一标识)
    //    redisCacheManager.setPrincipalIdFieldName("id");
    //    //用户权限信息缓存时间
    //    redisCacheManager.setExpire(200000);
    //    return redisCacheManager;
    //}

}

//package com.layblog.config;
//
//import com.layblog.shiro.AccountRealm;
//import com.layblog.shiro.LoginFilter;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.cache.CacheManager;
//import org.apache.shiro.mgt.SecurityManager;
//import org.apache.shiro.session.mgt.SessionManager;
//import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
//import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
//import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
//import org.crazycake.shiro.RedisCacheManager;
//import org.crazycake.shiro.RedisManager;
//import org.crazycake.shiro.RedisSessionDAO;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.servlet.Filter;
//import java.util.HashMap;
//import java.util.LinkedHashMap;
//import java.util.Map;
//
//@Slf4j
//@Configuration
//public class ShiroConfig {
//
//    @Value("${spring.redis.shiro.host}")
//    private String host;
//
//    @Value("${spring.redis.shiro.database}")
//    private int database;
//
//    /**
//     * shiro基础配置
//     *
//     * @param securityManager
//     * @return
//     */
//    @Bean
//    public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager securityManager) {
//
//        ShiroFilterFactoryBean filterFactoryBean = new ShiroFilterFactoryBean();
//        filterFactoryBean.setSecurityManager(securityManager);
//        // 配置登录的url和登录成功的url
//        filterFactoryBean.setLoginUrl("/login");
//        filterFactoryBean.setSuccessUrl("/user/center");
//        // 配置未授权跳转页面
//        filterFactoryBean.setUnauthorizedUrl("/error/403");
//
//        Map<String, Filter> filterMap = new HashMap<>();
//        filterMap.put("auth", loginFilter());
//
//        filterFactoryBean.setFilters(filterMap);
//
//        Map<String, String> hashMap = new LinkedHashMap<>();
//        //静态资源
//        hashMap.put("/res/**", "anon");
//
//        hashMap.put("/login", "anon");
//        hashMap.put("/post/edit", "auth");      //未登录提醒，这里的额auth其实是过滤器别名，也就是LoginFilter
//        hashMap.put("/post/delete", "auth");
//        hashMap.put("/post/submit", "auth");
//        hashMap.put("/post/reply", "auth");
//        hashMap.put("/post/jieda-delete", "auth");
//        hashMap.put("/center", "auth");
//        hashMap.put("/center/collection", "auth");
//        //hashMap.put("/admin/**", "auth, roles[admin]");
//        hashMap.put("/admin/initEsData", "anon");
//        filterFactoryBean.setFilterChainDefinitionMap(hashMap);
//
//        return filterFactoryBean;
//    }
//
//    /**
//     * 注入安全管理器
//     */
//    @Bean("securityManager")
//    public SecurityManager securityManager(AccountRealm accountRealm, SessionManager sessionManager, CacheManager cacheManager) {
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setSessionManager(sessionManager);
//        securityManager.setCacheManager(cacheManager);
//        securityManager.setRealm(accountRealm);
//        log.info("------------->securityManager注入完成");
//        return securityManager;
//    }
//
//    @Bean
//    public LoginFilter loginFilter() {
//        return new LoginFilter();
//    }
//
//    /**
//     * 配置shiro redisManager
//     * 使用的是shiro-redis开源插件
//     *
//     * @return
//     */
//    @Bean
//    public RedisManager redisManager() {
//        RedisManager redisManager = new RedisManager();
//        redisManager.setHost(host);
//        redisManager.setDatabase(database);
//        return redisManager;
//    }
//
//    @Bean
//    RedisSessionDAO redisSessionDAO() {
//        RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
//        redisSessionDAO.setRedisManager(redisManager());
//        return redisSessionDAO;
//    }
//
//    @Bean
//    DefaultWebSessionManager sessionManager(RedisSessionDAO redisSessionDAO) {
//        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
//        sessionManager.setSessionDAO(redisSessionDAO);
//        return sessionManager;
//    }
//
//    @Bean
//    RedisCacheManager redisCacheManager() {
//        RedisCacheManager redisCacheManager = new RedisCacheManager();
//        redisCacheManager.setRedisManager(redisManager());
//        //redis中针对不同用户缓存(此处的id需要对应user实体中的id字段,用于唯一标识)
//        redisCacheManager.setPrincipalIdFieldName("id");
//        //用户权限信息缓存时间
//        redisCacheManager.setExpire(200000);
//        return redisCacheManager;
//    }
//
//}
